package com.contextor.magiccards.web.amf;

import com.contextor.magiccards.model.errors.MagicCardsException;
import com.contextor.magiccards.web.handlers.auth.AuthToHandler;
import com.contextor.magiccards.web.handlers.battle.BattleToHandler;
import com.contextor.magiccards.web.handlers.character.CharacterToHandler;
import com.contextor.magiccards.web.to.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * User: sergey
 * Date: Mar 9, 2011 1:56:44 PM
 */
@Component
public abstract class BaseAmfService {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected AuthToHandler authHandler;
    @Autowired
    protected BattleToHandler battleHandler;
    @Autowired
    protected CharacterToHandler characterHandler;

    public <T> Response<T> onError(Throwable error) {
        logger.error(error.getMessage(), error);
        final Response<T> response = new Response<T>();
        if (error instanceof MagicCardsException) {
            response.errorCode = ((MagicCardsException) error).getCode().name();
            response.errorDescription = error.toString();
        } else {
            response.errorCode = error.getMessage();
        }
        return response;
    }


}
